package com.hht.floatbar.service;

import android.content.Intent;
import android.net.VpnService;
import android.os.ParcelFileDescriptor;

import java.net.DatagramSocket;
import java.net.DatagramSocketImpl;
import java.net.DatagramSocketImplFactory;
import java.net.InetAddress;
import java.net.SocketException;

public class DvpnService extends VpnService {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        System.out.println("服务启动完毕");
        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    DatagramSocket datagramSocket = new DatagramSocket();
                    protect(datagramSocket);
                    datagramSocket.connect(InetAddress.getByName("192.168.1.86"), 80);
                    // Configure a new interface from our VpnService instance. This must be done
                    // from inside a VpnService.
                    VpnService.Builder builder = new VpnService.Builder();

                    // Create a local TUN interface using predetermined addresses. In your app,
                    // you typically use values returned from the VPN gateway during handshaking.
                    ParcelFileDescriptor localTunnel = builder
                            .addAddress("192.168.2.2", 24)
                            .addRoute("0.0.0.0", 0)
                            .addDnsServer("192.168.1.1")
                            .establish();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        };
        thread.start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("服务关闭");
    }
}
